﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace BDSim
{
    /// <summary>
    /// 地球自转模型
    /// </summary>
    public class EarthAutoTurnModel : AstronomyEnvModel
    {
        #region 字段和属性
        /// <summary>
        /// 右键命令集合
        /// </summary>
        private List<MenuCommand> MenuCommandList = new List<MenuCommand>();
        /// <summary>
        /// 文件参数名  
        /// </summary>
        private string _FileName = currentDirectory + "\\ConfigFiles\\EOP_BDSim.txt";
        [XmlAttribute, ReadOnly(true), CategoryAttribute("模型设置")]
        public string FileName
        {
            get { return _FileName; }
            set { _FileName = value; }
        }

        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public EarthAutoTurnModel()
        {
            MenuCommand LoadFile = new MenuCommand();
            LoadFile.Header = "加载EOP文件";
            LoadFile.DefineExcuteCommand(ShowLoadFileDialog_Execute, ShowLoadFileDialog_CanExecute);
            MenuCommandList.Add(LoadFile);

            Menu.ItemsSource = MenuCommandList;
        }

        #endregion

        #region 方法
        /// <summary>
        /// 显示加载文件对话框
        /// </summary>
        /// <param name="param"></param>
        private void ShowLoadFileDialog_Execute(object param)
        {
            LoadAstronomyEnvFile loadFile = new LoadAstronomyEnvFile(2);
            loadFile.FileName = this.FileName;
            loadFile.ShowDialog();
            if (loadFile.SelResult == true)
            {
                this.FileName = loadFile.FileName;
                //add by liuy on 20160803
                ReadEopFile();
            }
        }
        private bool ShowLoadFileDialog_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 初始化函数
        /// </summary>
        public override bool Init()
        {
            return ReadEopFile();
        }

        /// <summary>
        /// 读极移跳秒文件
        /// </summary>
        /// <returns></returns>
        unsafe private bool ReadEopFile()
        {
            UTCTime start = BDSimObject.GlobalStartTime;
            UTCTime end = BDSimObject.GlobalEndTime;
            
            bool ret = BDSim.ReadEOP.EOP_Init(FileName.ToCharArray(), start, end);
            if (ret == true)
            {
                int count = 0;
                EOPData* pdata = BDSim.ReadEOP.EOP_GetEopData(out count);
                //时空类动态库初始化
                BDSim.TimeSpaceDll.TimeSpaceInit(pdata, count);

            }
            else
            {
                MessageBox.Show("读极移跳秒文件失败！", "错误");
                return false;
            }

            return true;
        }

        #endregion
    }
}
